package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.specification.TbSpecification;
import com.dongdongshop.pojo.specification.TbSpecificationExample;
import com.dongdongshop.pojo.specification.TbSpecificationOption;
import com.dongdongshop.pojo.specification.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecificationServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Service
public class SpecificationServiceImpl implements SpecificationServiceI {

    private final Logger logger= LoggerFactory.getLogger(SpecificationServiceImpl.class);

    @Resource
    private TbSpecificationMapper specificationMapper;
    @Resource
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TbSpecification> getSpecs() {
        return specificationMapper.selectByExample(null);
    }

    @Override
    public int updateSpec(TbSpecification specification, List<TbSpecificationOption> specificationOptions) {
        try {
            specificationMapper.updateByPrimaryKeySelective(specification);
            //先删除在增加
            TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = tbSpecificationOptionExample.createCriteria();
            criteria.andSpecIdEqualTo(specification.getId());
            tbSpecificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
            tbSpecificationOptionMapper.insertBatch(specification.getId(),specificationOptions);
            return 1;
        }catch (Exception e){
            logger.error("操作异常",e);
            return 0;
        }
    }

    @Override
    public TbSpecification getSpecById(Long id) {
        TbSpecification specification = specificationMapper.selectByPrimaryKey(id);
        List<TbSpecificationOption> optionList=tbSpecificationOptionMapper.selectSpecs(id);
        specification.setList(optionList);
        return specification;
    }

    @Override
    public int insertSpecs(TbSpecification specification, List<TbSpecificationOption> specificationOptions) {
      try {
          //增加数据 并返回id
          specificationMapper.insertSpec(specification);
          //System.out.println("id"+specification.getId());
          //System.out.println("id"+specification.getSpecName());
          //增加specoption
          tbSpecificationOptionMapper.insertBatch(specification.getId(),specificationOptions);

          return 1;
      }catch (Exception e){
          logger.error("操作异常",e);
          return 0;
      }
    }

    @Override
    public int deleteSpec(Integer[] ids) {
        try {
            specificationMapper.deleteSpecByIds(ids);
            tbSpecificationOptionMapper.deleteSpecOByIds(ids);
            return 1;
        }catch (Exception e){
            logger.error("操作异常",e);
           return 0;
        }

    }

    @Override
    public List<TbSpecification> getSpecName(String specName) {
        return specificationMapper.getSpecName(specName);
    }
}
